//
//  WYRMessageCell.m
//  10-21-快速搭建框架
//
//  Created by zzn on 2016/10/31.
//  Copyright © 2016年 lkx0. All rights reserved.
//

#import "WYRMessageCell.h"
#import "WYRMessage.h"
#import "WYRMeaasgeframe.h"
//添加三个控件 - 自定义cell中放置
@interface WYRMessageCell()

//1.时间cell
@property(nonatomic,weak)UILabel *wyrTime;

//2.消息正文button
@property(nonatomic,weak)UIButton *btnText;

//3.头像image
@property(nonatomic,weak)UIImageView *imgViewIcon;

@end

@implementation WYRMessageCell



    //如果要自定义cell  必须重写UITableViewCell的initWithStyle方法

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            //实现我们自己定义的cell的内容
            //比如我们需要在自己定义的cell里放置那些控件
            //1.添加时间控件
            UILabel * wyrTime=[[UILabel alloc]init];
            
            //2.设置文字大小
            wyrTime.font=[UIFont systemFontOfSize:12];
            
            //3.设置文字居中
            wyrTime.textAlignment=NSTextAlignmentCenter;
            [self.contentView addSubview:wyrTime];
            
            self.wyrTime=wyrTime;
            
            //2.显示头像的image
            UIImageView *headImage=[[UIImageView alloc]init];
            
            [self.contentView addSubview:headImage];
            
            _imgViewIcon=headImage;
            
            //3. 正文按钮
            UIButton *textBtn=[[UIButton alloc]init];
            
            //设置正文的文字大小
            textBtn.titleLabel.font=[UIFont systemFontOfSize:14];
            
            //设置文字的颜色
            [textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            //设置正文换行
            textBtn.titleLabel.numberOfLines=0;
            
            //设置按钮的背景色
          //  textBtn.backgroundColor=[UIColor purpleColor];
          //  textBtn.titleLabel.backgroundColor=[UIColor greenColor];
            //设置按钮的内边距
            textBtn.contentEdgeInsets=UIEdgeInsetsMake(15, 20, 15, 20);
            
            [self.contentView addSubview:textBtn];
            
            _btnText=textBtn;
            
        }
    self.backgroundColor=[UIColor clearColor];
    return self;
    }


//重写frame模型的set方法  设置每个控件的frrame和数据
-(void)setMessageFrame:(WYRMeaasgeframe *)messageFrame{
    _messageFrame=messageFrame;
    //1.获取数据模型
    WYRMessage * message=messageFrame.message;
    
    //2.分别设置frame和数据
    //2.1设置时间lable和frame和数据
    _wyrTime.text=message.time;
    _wyrTime.frame=messageFrame.timeframe;
    
    //2.2设置头像的数据和frame
    _imgViewIcon.frame=messageFrame.iconframe;
    
    //头像图片根据消息发送类别type确定
    NSString *iconName=(message.type==WYRMessageTypeMe)?@"me":@"other";
    _imgViewIcon.image=[UIImage imageNamed:iconName];
    
    //2.3设置正文的数据和frame
    [_btnText setTitle:message.text forState:UIControlStateNormal];
    _btnText.frame=messageFrame.textFrame;
    

    //2.3.2设置正文
    //判断消息发送者是谁
    NSString *ImgNor,*ImgHighLighted;
    if (message.type==WYRMessageTypeMe) {
        ImgNor=@"chat_send_nor";
        ImgHighLighted=@"chat_send_press_pic";
        
        //设置自己发送的消息
        [self.btnText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }else{
        ImgNor=@"chat_recive_nor";
        ImgHighLighted=@"chat_recive_press_nor";
        
        [self.btnText setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    }
    
    //设置正文的size来修改气泡图片防止变形
    UIImage *imageNor=[UIImage imageNamed:ImgNor];
    
    UIImage *imageHighLighted=[UIImage imageNamed:ImgHighLighted];
    
    
    //用平铺的方式去拉伸图片（四个角分开）
    imageNor=[imageNor stretchableImageWithLeftCapWidth:imageNor.size.width*0.5 topCapHeight:imageNor.size.height*0.5];
    
    imageHighLighted=[imageHighLighted stretchableImageWithLeftCapWidth:imageHighLighted.size.width*0.5 topCapHeight:imageHighLighted.size.height*0.5];
    
    
    //设置正文的背景气泡图片
    [self.btnText setBackgroundImage:imageNor forState:UIControlStateNormal];
    
    [self.btnText setBackgroundImage:imageHighLighted forState:UIControlStateHighlighted];
    
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
